import json
d = [{"姓名":"张三","年龄":20},{"姓名":"李四","年龄":18}]
# 写入json
with open("test.json","w+",encoding="utf-8") as f:
    f.write(json.dumps(d,ensure_ascii=False,indent=2))
t = json.dumps(d,ensure_ascii=False,indent=2)
print(type(t))
with open("test1.json","w+",encoding="utf-8") as f:
    json.dump(d,f,ensure_ascii=False,indent=2)
# 读取json
# 在字符串中读取json
t1 = '[{"姓名":"张三","年龄":20},{"姓名":"李四","年龄":18}]'
d1 = json.loads(t1)
print(d1)
print(type(d1))
print(d1[0].values(),t1[0])
# 在文件中读取json
with open("test.json","r",encoding="utf-8") as f:
    d2 = json.load(f)
    print(d2,type(d2))